//
//  changePassWordViewController.m
//  DongFeng_Online-Banking-APP
//
//  Created by wxt on 2021/3/30.
//

#import "ChangePassWordViewController.h"
#import "ViewController.h"
#import "ChangeSuccessViewController.h"
#import "LognViewController.h"
@interface ChangePassWordViewController ()<UITextFieldDelegate,NoNetworkViewDelegate>
@property(nonatomic,strong)TKLtextFiled * payWordF;
@property(nonatomic,strong)TKLtextFiled * rePayWordF;
@property(nonatomic,strong)TKLlable * payWordMessageLab;
@property(nonatomic,strong)TKLlable * rePayWordMessageLab;
@property(nonatomic,strong)UIView * payLinView;
@property(nonatomic,strong)UIView * rePayLinView;
@property(nonatomic,strong)UIButton * isHiddenBtn;

@property(nonatomic,strong)MBProgressHUD *hud;
@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Wcolor;

    UIButton * backBtn =[UIButton createBtnFrame:CGRectMake(15, kStatusBarHeight+5, 18, 18) title:@"" bgImageName:@"fanhui" action:^(UIButton *button) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [self.view addSubview:backBtn];
    TKLlable * navTit = [TKLlable labelWithFrame:CGRectMake(kWidth/2-60, kStatusBarHeight+5, 120, 20) text:@"" textColor:Bcolor font:Font_6(16) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:navTit];

        navTit.text= @"修改登录密码";

    
    [self  loadUIView];
    // Do any additional setup after loading the view.
}
-(void)loadUIView{
    
   
   
    
    
    TKLlable * messLab = [TKLlable labelWithFrame:CGRectMake(30, 140, kWidth-60, 16) text:@"请设置新的登录密码" textColor:Bcolor font:Font_6(18) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:messLab];
    
    TKLlable * passMessLab = [TKLlable labelWithFrame:CGRectMake(30, CGRectGetMaxY(messLab.frame)+10, kWidth-60, 16) text:@"登录密码为8-16位数字和字母，需包含大小写" textColor:JhColor(204, 204, 204) font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:passMessLab];
    
//    UIButton * messBtn = [UIButton createBtnFrame2:CGRectMake(30, CGRectGetMaxY(lognLab.frame)+30, kWidth-60, 16) title:@"为确保资金安全，请重新设置登录密码" bgImageName:@"" action:^(UIButton *button) {
//        ;
//    }];
//    messBtn.titleLabel.textAlignment = Textleft;
//    [self.view addSubview:messBtn];
    

    self.payWordF = [TKLtextFiled FiledWithFrame:CGRectMake(30, CGRectGetMaxY(passMessLab.frame)+30, kWidth-120, 20) text:@"请输入8-16位数支付密码" textColor:Bcolor font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor];
//    self.payWordF.keyboardType = UIKeyboardTypeNumberPad;
    self.payWordF.delegate = self;
    [self.payWordF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.payWordF.secureTextEntry = YES;
    [self.view addSubview:self.payWordF];
    
    self.payWordMessageLab = [TKLlable labelWithFrame:CGRectMake(30, CGRectGetMaxY(passMessLab.frame)+20, kWidth-120, 18) text:@"" textColor:[UIColor redColor] font:Font_6(12) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:self.payWordMessageLab];
    
    
    
    //密文切换
    
   self.isHiddenBtn = [UIButton createBtnFrame:CGRectMake(CGRectGetMaxX(self.payWordF.frame)+30, CGRectGetMaxY(passMessLab.frame)+30, 30, 20) title:@"" bgImageName:@"yincang" action:^(UIButton *button) {
        ;
        
        [self ishiddenAction];
        
    }];
    self.isHiddenBtn.selected = YES;
    [self.isHiddenBtn setTitleColor:Bcolor forState:UIControlStateNormal];
    [self.view addSubview:self.isHiddenBtn];
    
    
 
    
    
    self.payLinView = [[UIView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.payWordF.frame)+6, kWidth-60, 1)];
    self.payLinView.backgroundColor = JhColor(153, 153, 153);
    [self.view addSubview:self.payLinView];
    
    
    self.rePayWordF = [TKLtextFiled FiledWithFrame:CGRectMake(30, CGRectGetMaxY(self.payLinView.frame)+30, kWidth-120, 20) text:@"请再次输入8-16位数登录密码" textColor:Bcolor font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:self.rePayWordF];
    self.rePayWordF.delegate = self;
    [self.rePayWordF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.rePayWordF.secureTextEntry = YES;
    self.rePayLinView = [[UIView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.rePayWordF.frame)+6, kWidth-60, 1)];
    self.rePayLinView.backgroundColor = JhColor(153, 153, 153);
    
    [self.view addSubview:self.rePayLinView];
    
    //提示信息
    self.rePayWordMessageLab = [TKLlable labelWithFrame:CGRectMake(30, CGRectGetMaxY(self.payLinView.frame)+20, kWidth-60, 14) text:@"" textColor:[UIColor redColor] font:Font_6(12) textAlignment:Textleft backgroundColor:Ccolor];
//    self.payWordMessageLab.hidden = YES;
    [self.view addSubview:self.rePayWordMessageLab];
    
    //next
    UIButton * nextBtn = [UIButton createBtnFrame:CGRectMake(30, CGRectGetMaxY(self.rePayLinView.frame)+30, kWidth-60, 44) title:@"确定" bgImageName:@"" action:^(UIButton *button) {
        [self.view endEditing:YES];
        
        self.payWordF.text = [self.payWordF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        self.rePayWordF.text = [self.rePayWordF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
     
        if ([TKLTools isPassWord:self.payWordF.text]) {
            
            if ([TKLTools isPassWord:self.rePayWordF.text]) {
                if ([self.payWordF.text isEqualToString:self.rePayWordF.text]) {
                    self.rePayWordMessageLab.hidden = YES;
                    self.payWordMessageLab.hidden = YES;
                    self.rePayLinView.backgroundColor = JhColor(225, 225, 225);
                    self.payLinView.backgroundColor = JhColor(225, 225, 225);
                        //修改
                            [self changePassWordUrl];
              
                }else{
                    self.rePayLinView.backgroundColor = JhColor(239, 45, 47);
                    self.rePayWordMessageLab.text = @"两次密码输入不一致";
                    self.rePayWordMessageLab.hidden = NO;
                }
                
            }else{
                self.rePayLinView.backgroundColor = JhColor(239, 45, 47);
                self.rePayWordMessageLab.text = @"请输入8-16位数登录密码";
                self.rePayWordMessageLab.hidden = NO;
            }
            
          
        }else{
            self.payLinView.backgroundColor = JhColor(239, 45, 47);
            self.payWordMessageLab.text = @"请输入8-16位数登录密码";
            self.payWordMessageLab.hidden = NO;
            
        }
        
    }];
//    nextBtn
    nextBtn.backgroundColor = JhColor(21, 126, 251);
    nextBtn.layer.masksToBounds = YES;
    nextBtn.layer.cornerRadius = 22;
    [self.view addSubview:nextBtn];
   
}


-(void)textFieldDidBeginEditing:(UITextField *)textField{
  
    if (textField == self.payWordF) {
        self.payLinView.backgroundColor = JhColor(225, 225, 225);
        self.rePayLinView.backgroundColor = JhColor(225, 225, 225);
        self.payWordMessageLab.hidden = YES;
        self.rePayWordMessageLab.hidden = YES;
    }else{
        
        self.rePayLinView.backgroundColor = JhColor(225, 225, 225);
        self.payLinView.backgroundColor = JhColor(225, 225, 225);
        self.rePayWordMessageLab.hidden = YES;
        self.payWordMessageLab.hidden = YES;
    }
  
    
}
//监听长度
- (void)textFieldDidChange:(UITextField *)textField{
   
    if (textField == self.payWordF) {
        if (textField.text.length > 16) {
            self.payWordF.text = [textField.text substringToIndex:16];
        }
        if (textField.text.length == 0) {
            self.payWordMessageLab.hidden = YES;
        }
    }else if(textField == self.rePayWordF){
        if (textField.text.length > 16) {
            self.rePayWordF.text = [textField.text substringToIndex:16];
        }
        if (textField.text.length == 0) {
            self.rePayWordMessageLab.hidden = YES;
        }
    }

}

-(void)ishiddenAction{
    // 取反

    if (self.isHiddenBtn.selected == NO) {
        self.isHiddenBtn.selected = YES;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"xianshi"] forState:UIControlStateNormal];
    }else{
        self.isHiddenBtn.selected = NO;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"yincang"] forState:UIControlStateNormal];
    }
    
    
    self.payWordF.secureTextEntry = !self.payWordF.secureTextEntry;
    self.rePayWordF.secureTextEntry = !self.rePayWordF.secureTextEntry;
// 获得输入的值

    NSString *content = self.payWordF.text;
    NSString *recontent = self.rePayWordF.text;
 // 解决输入框从密文切换明文时出现空格的问题

    self.payWordF.text = @"";
    self.rePayWordF.text = @"";

    self.payWordF.text = content;
    self.rePayWordF.text = recontent;
 //如果是密文转明文进行二次编辑不会出现清空现象,如果是明文转密文进行二次编辑就会出现清空现象,进行判断,如果切换后此时输入框是密文状态,就保存原有内容并赋值给输入框,这样就不会出现清空现象了

   if (self.payWordF.secureTextEntry) {

 // 解决输入框从明文切换为密文时进行二次编辑出现清空现象

       [self.payWordF insertText:content];
       [self.rePayWordF insertText:recontent];

 }

 
    
    
}
//修改
-(void)changePassWordUrl{
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
      self.hud.labelText = NSLocalizedString(@"请稍等", @"HUD loading title");
    
    NSUserDefaults * stand = [NSUserDefaults standardUserDefaults];
    
    NSDictionary * dic = @{@"newPassword":self.payWordF.text,@"username":[stand objectForKey:@"userName"]};


    [AFNetworkUtils POST_RawToken:kUser_Change_PassWord_Api body:dic.yy_modelToJSONString head:nil success:^(NSDictionary * _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
        self.hud.hidden = YES;
        if ([responseObject[@"code"] intValue] == 200) {
            
//            [MBProgressHUD showMessage:@"修改成功，请重新登录" toView:self.view];
//            [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
        
            ChangeSuccessViewController * successVC = [[ChangeSuccessViewController alloc]init];
            successVC.modalPresentationStyle = 0;
            [self presentViewController:successVC animated:YES completion:nil];
            
            
            
        }else if ([responseObject[@"code"] intValue] == 904) {
            //新旧密码相同
            [MBProgressHUD showMessage:@"新旧密码不能相同" toView:self.view];
        }else if ([responseObject[@"code"] intValue] == 401) {
            //token过期
            LognViewController * viewVC = [[LognViewController alloc]init];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"tokenNotime" object:@""];
            [UIApplication sharedApplication].keyWindow.rootViewController = viewVC;
          
        }else{
            [MBProgressHUD showMessage:responseObject[@"msg"] toView:self.view];
            
        }
        } failure:^(NSString * _Nonnull error) {
            NSLog(@"%@",error);
            self.hud.hidden = YES;
        }];
    
  
    
}
-(void)dealloc{
  
    
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
 
    
}
-(void)delayMethod{
   
            UIViewController *rootVC = self.presentingViewController;
            while(rootVC.presentingViewController) {
            rootVC= rootVC.presentingViewController;
            }
            
         [rootVC dismissViewControllerAnimated:YES completion:nil];
  
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
